<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
</head>

<body>
    <div class="box">
        <div class="aaa">
            <div>
                <p class="aaa">
                    <span>后代元素</span>
                </p>
            </div>
        </div>

    </div>


    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        var $span = $('span');
        // 查找span元素的所有的祖宗元素 
        var $parents = $span.parents();
        console.log('$parents', $parents);


        // 查找span元素祖宗元素里离它最近的类名为aaa的元素
        var $aaa = $span.closest('.aaa');
        console.log('$aaa', $aaa);
    </script>
</body>

</html>